<template>
    <div>
      <div style="margin-bottom: 20px">
        <Row>
          <Col span="5">
            <span>事项：</span>
            <Select v-model="params.Row_ID" style="width: 75%;" filterable>
              <Option v-for="(item,index) in itemList1" :value="item.Row_ID" :key="index">{{item.QL_NAME}}</Option>
            </Select>
          </Col>
          <span style="margin-left: 20px">业务部门：</span>
          <Select v-model="params.ju" style="width:20%">
            <Option v-for="(item,index) in itemTypeList" :value="item.name" :key="index">{{ item.name }}</Option>
          </Select>
          <Button type="primary" @click="selectList(1)"  style="margin-right:10px;margin-left: 20px"><Icon type="ios-search" />搜索</Button>
          <Button type="primary" @click="add(0)"><Icon type="md-add" />添加</Button>
        </Row>
      </div>
      <div style="margin-bottom: 20px">
        <Table border :columns="columns" :data="data">
          <template slot-scope="{ row, index }" slot="action">
            <Button type="primary" size="small" style="margin-right: 5px" @click="detail(row,1)">详情</Button>
            <Button type="success" size="small" @click="update(row,2)" >修改</Button>
          </template>
        </Table>
      </div>
      <div style="text-align: right">
        <Page :total="params.total" :page-size="params.pageSize" show-sizer show-total show-elevator @on-change="pageChange" @on-page-size-change="pageSizeChange"/>
      </div>
      <Modal
        v-model="addModal"
        title="添加咨询">
        <template>
          <Form :model="formItem" :label-width="80">
            <FormItem label="标题:">
              <Input v-model="formItem.title" placeholder="标题"></Input>
            </FormItem>
            <FormItem label="问题内容：">
              <Input v-model="formItem.questioncontent" type="textarea" :autosize="{minRows: 2,maxRows: 5}" placeholder="问题内容"></Input>
            </FormItem>
            <FormItem label="回复内容：">
              <Input v-model="formItem.answercontent" type="textarea" :autosize="{minRows: 2,maxRows: 5}" placeholder="回复内容"></Input>
            </FormItem>
            <FormItem label="姓名:">
              <Input v-model="formItem.name" placeholder="姓名"></Input>
            </FormItem>
            <FormItem label="身份证号:">
              <Input v-model="formItem.cardno" placeholder="身份证号"></Input>
            </FormItem>
            <FormItem label="手机号:">
              <Input v-model="formItem.moblie" placeholder="手机号"></Input>
            </FormItem>
            <FormItem label="事项名称:">
              <Select v-model="formItem.Row_ID" filterable>
                <Option v-for="(item,index) in itemList" :value="item.Row_ID" :key="index">{{item.QL_NAME}}</Option>
              </Select>
            </FormItem>
          </Form>
        </template>
        <div slot="footer">
          <Button v-if="this.type===0" type="primary"  @click="addOk">添加</Button>
          <Button v-if="this.type===2" type="primary"  @click="updateOk">修改</Button>
        </div>
      </Modal>
    </div>
</template>

<script>
import { findGuestionConsultationMyList, findItemList, createQuestionConsultation, byIdInfo, updateInfoById, createKnowledge, itemTypeList } from '@/api/eventlist'

export default {
  name: 'index',
  data () {
    return {
      itemTypeList: [],
      formItem: {},
      columns: [
        {
          title: '编号',
          key: 'id'
        },
        {
          title: '标题',
          key: 'title'
        },
        {
          title: '姓名',
          key: 'name'
        },
        {
          title: '身份证号',
          key: 'cardno'
        },
        {
          title: '手机号',
          key: 'moblie'
        },
        {
          title: '事项名称',
          key: 'QL_NAME'
        },
        {
          title: '创建人',
          key: 'create_by'
        },
        {
          title: '创建时间',
          key: 'create_time'
        },
        {
          title: '操作',
          slot: 'action'
        }

      ],
      data: [],
      itemList: [],
      params: { total: 0, pageNo: 1, pageSize: 10, Row_ID: '', ju: '' },
      addModal: false,
      type: 0, // 0 新增   1  详情   2 修改
      item: '',
      itemList1: []
    }
  },
  methods: {
    itemTypeList1 () {
      let data = {
        show_state: 'pc%'
      }
      itemTypeList(data).then(ret => {
        this.itemTypeList = ret.data.data
      })
    },
    add (e2) {
      this.formItem = {}
      this.type = e2
      this.addModal = true
    },
    update (e, e2) {
      this.formItem = e
      this.type = e2
      this.addModal = true
    },
    updateOk () {
      this.itemList.forEach((item, index) => {
        if (item.Row_ID === this.formItem.Row_ID) {
          this.formItem.QL_NAME = item.QL_NAME
        }
      })
      let data = {
        'id': this.formItem.id,
        'title': this.formItem.title,
        'questioncontent': this.formItem.questioncontent,
        'answercontent': this.formItem.answercontent,
        'name': this.formItem.name,
        'cardno': this.formItem.cardno,
        'moblie': this.formItem.moblie,
        'Row_ID': this.formItem.Row_ID,
        'QL_NAME': this.formItem.QL_NAME
      }
      updateInfoById(data).then(ret => {
        if (ret.data.errcode === 0) {
          this.$Notice.success({
            title: '修改成功',
            desc: ret.data.errmsg
          })
          this.addModal = false
          this.formItem = {}
          this.init()
        } else {
          this.$Notice.error({
            title: '修改失败',
            desc: ret.data.errmsg
          })
        }
      })
    },
    detail (e, e2) {
      this.type = e2
      this.addModal = true
      let data = {
        'id': e.id
      }
      byIdInfo(data).then(ret => {
        if (ret.data.errcode === 0) {
          this.formItem = ret.data.data
        } else {
          this.$Notice.error({
            title: '查询列表失败',
            desc: ret.data.errmsg
          })
        }
      })
    },
    addOk () {
      this.itemList.forEach((item, index) => {
        if (item.Row_ID === this.formItem.Row_ID) {
          this.formItem.QL_NAME = item.QL_NAME
        }
      })
      let data = {
        'title': this.formItem.title,
        'questioncontent': this.formItem.questioncontent,
        'answercontent': this.formItem.answercontent,
        'name': this.formItem.name,
        'cardno': this.formItem.cardno,
        'moblie': this.formItem.moblie,
        'Row_ID': this.formItem.Row_ID,
        'QL_NAME': this.formItem.QL_NAME
      }
      createQuestionConsultation(data).then(ret => {
        if (ret.data.errcode === 0) {
          this.$Notice.success({
            title: '创建成功',
            desc: ret.data.errmsg
          })
          this.createKnowledge()
          this.addModal = false
          this.formItem = {}
          this.init()
        } else {
          this.$Notice.error({
            title: '创建失败',
            desc: ret.data.errmsg
          })
        }
      })
    },
    createKnowledge () {
      let data = {
        type: '1',
        knowledge_type: '问题咨询',
        knowledge_title: this.formItem.QL_NAME + '-----' + this.formItem.questioncontent,
        knowledge_content: this.formItem.answercontent
      }
      createKnowledge(data).then(ret => {
        if (ret.data.errcode === 0) {
          this.$Notice.success({
            title: '创建成功',
            desc: ret.data.errmsg
          })
        } else {
          this.$Notice.error({
            title: '创建失败',
            desc: ret.data.errmsg
          })
        }
      })
    },
    selectList (e) {
      this.params.pageNo = e
      this.init()
    },
    findItemList () {
      findItemList().then(ret => {
        if (ret.data.errcode === 0) {
          this.itemList = ret.data.data
          this.itemList1 = ret.data.data
        }
      })
    },
    init () {
      let data = this.params
      findGuestionConsultationMyList(data).then(ret => {
        if (ret.data.errcode === 0) {
          this.params.total = ret.data.data.totalRecord
          this.data = ret.data.data.results
        } else {
          this.$Notice.error({
            title: '查询列表失败',
            desc: ret.data.errmsg
          })
        }
      })
    },
    pageChange (page) {
      this.params.pageNo = page
      this.init()
    },
    pageSizeChange (e) {
      this.params.pageSize = e
      this.init()
    }
  },
  mounted () {
    this.init()
    this.findItemList()
    this.itemTypeList1()
  }
}
</script>

<style scoped>

</style>
